﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Crawler.DataHelper
{
    public static class RegexHelper
    {
        public static string regexDaYuHao = @"(?:(?:<a[^>]*>[^<>]*(((?'Open'<a[^>]*>)[^<>]*)+((?'-Open'</a>)[^<>]*)+)*(?(Open)(?!))</a>\s*)+((?<=>)\s+>[^<>]+(?=<)))+(?:<a[^>]*>[^<>]*(((?'ga'<a[^>]*>)[^<>]*)+((?'-ga'</div>)[^<>]*)+)*(?(ga)(?!))</a>\s*)?";
        public static string regexEmLabel = @"&gt;";
//@"(?:(?:<a[^>]*>[^<>]*(((?'Open'<a[^>]*>)[^<>]*)+((?'-Open'</a>)[^<>]*)+)*(?(Open)(?!))</a>[^<>]+)+<em[^>]*>.*(((?'gem'<em[^>]*>)[^<>]*)+((?'-gem'</em>)[^<>]*)+)*(?(gem)(?!))</em>\s*)+(?:<a[^>]*>[^<>]*(((?'ga'<a[^>]*>)[^<>]*)+((?'-ga'</div>)[^<>]*)+)*(?(ga)(?!))</a>\s*)+";
        public static string regexBoultSign = @"((<a[^>]*>[^<>]*(((?'Open'<a[^>]*>)[^<>]*)+((?'-Open'</a>)[^<>]*)+)*(?(Open)(?!))</a>(<br>)?.*?</a>)(\s*→[^→<>]+(?=<))\s*)+(<a[^>]*>[^<>]*(((?'ga'<a[^>]*>)[^<>]*)+((?'-ga'</a>)[^<>]*)+)*(?(ga)(?!))</a>\s*)";
        public static string regexCream = @"((?<=>)\s*(精华|精品)\s*(?=<))|(?<=>)\s*>\s*列表\s*(?=<)";
        public static string regexDayuhao_zhengwen = @"(?<=</[^<>]+>)\s*>\s*(<(?'style'[^<>]+)>)?\s*正文\s*(</\k<style>>\s*)?";
        public static string regexSofa = @"((?<=>)\s*沙发\s*(?=<))|(第?\s*(<[^<>]+>)?{[^{}]+}(<[^<>]+>)?\s*楼)|(?'floor'第?\s*(<[^<>]+>)?(?<![0-9])\s*[12](?![0-9])\s*(<[^<>]+>)?\s*楼)";
        public static string regexPostTitle = @"(?<=title\>).*(?=</title)";
        public static string regexPostTime = @"(?:(?!0000)[0-9]{4}-(?:(?:0?[1-9]|1[0-2])-(?:0?[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)\s+([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]";
        public static string regexPostContent = @"<div[^>]*>[^<>]*(((?'Div'<div[^>]*>)|(?'-Div'</div>)|(<style[^>]*>)|(</style>)|(<script[^>(]*(\([^<>]*[<>]?[^<>]*)*>)|(</script>)|(<ignore_js_op[^>]*>)|(</ignore_js_op>)|(<table[^>]*>)|(</table>)|(<dl[^>]*>)|(</dl>)|(<dt[^>]*>)|(</dt>)|(<dd[^>]*>)|(</dd>)|(<tr[^>]*>)|(</tr>)|(<td[^>]*>)|(</td>)|(<font[^>]*>)|(</font>)|(<br\s*\/>)|(<br[^>]*>)|(<strong[^>]*>)|(</strong>)|(<spa[^>]*>)|(</span>)|(<a[^>]*>)|(</a>)|(<p\b[^>]*>)|(</p>)|(<b\b[^>]*>)|(</b>)|(<i\b+[^>]*>)|(</i>)|(<tbody[^>]*>)|(</tbody>)|(<em\b[^>]*>)|(</em>)|(<img\b[^>]*/>)|(<img\b[^>(]*(\([^<>]*[<>]?[^<>]*)*>))(\r*\n*(?<Text>((?<=>)[^<>]*(\s*(&nbsp;)*\d*\s*(&nbsp;)*\w*\s*(&nbsp;)*\d*\s*(&nbsp;)*[-—+，,。.、%《》…【】？?!！'‘’""“”()（）；:：;~/]+\s*(&nbsp;)*){3,}[^<>]*(?=(<))))|(?'else'[^<>]*)))+(?(Text)|(?!))</div>";
        public static string regexPostContentWithoutLabel = @"";
        public static string regexPostContent1 = @"<td.class='t_f' id='postmessage_.+?'>\s.+?(\s)*</td></tr></table>";
        public static string regexPostContent2 = @"<!--content_read-->.+?<!--content_read-->";
        public static string regexPostContent3 = @"<div class='bbs-content clearfix'>.+?<div id='host_per_info'";
        public static string regexPostContent4 = @"<TD class=postcontent>.+?<TD class=postcontent>";
     
        /// <summary>
        /// 判断是否有“〉”符号
        /// </summary>
        /// <param name="strResponse">网页源代码</param>
        /// <returns></returns>
        public static bool HasDaYuHao(string strResponse)
        {
            bool hasDaYuHao = false;
            MatchCollection matches = new Regex(regexDaYuHao).Matches(strResponse);
            if (matches.Count != 0)
                hasDaYuHao = true;
            return hasDaYuHao;
        }
        /// <summary>
        /// 判断是否<em></em>标签
        /// </summary>
        /// <param name="strResponse">网页源代码</param>
        /// <returns></returns>
        public static bool HasEmLabel(string strResponse)
        {
            bool hasEmLabel = false;
            MatchCollection matches = new Regex(regexEmLabel).Matches(strResponse);
            if (matches.Count != 0)
                hasEmLabel = true;
            return hasEmLabel;
        }
        /// <summary>
        /// 判断时候有箭头符号
        /// </summary>
        /// <param name="strResponse">网页源代码</param>
        /// <returns></returns>
        public static bool HasBoultSign(string strResponse)
        {
            bool hasBoultSign = false;
            MatchCollection matches = new Regex(regexBoultSign).Matches(strResponse);
            if (matches.Count != 0)
                hasBoultSign = true;
            return hasBoultSign;

        }
        /// <summary>
        /// 判断是否有〉正文存在
        /// </summary>
        /// <param name="strResponse"></param>
        /// <returns></returns>
        public static bool HasDayuhao_zhengwen(string strResponse)
        {
            bool hasDayuhao_zhengwen = false;
            MatchCollection matches = new Regex(regexDayuhao_zhengwen).Matches(strResponse);
            if (matches.Count != 0)
                hasDayuhao_zhengwen = true;
            return hasDayuhao_zhengwen;
        }
        /// <summary>
        /// 判断是否有精华
        /// </summary>
        /// <param name="strResponse"></param>
        /// <returns></returns>
        public static bool HasCream(string strResponse)
        {
            bool hasCream = false;
            MatchCollection matches = new Regex(regexCream).Matches(strResponse);
            if (matches.Count != 0)
                hasCream = true;
            return hasCream;
        }
        /// <summary>
        /// 判断是否有沙发或一楼
        /// </summary>
        /// <param name="strResponse"></param>
        /// <returns></returns>
        public static bool HasSofa(string strResponse)
        {
            bool hasSofa = false;
            MatchCollection matches = new Regex(regexSofa).Matches(strResponse);
            if (matches.Count != 0)
                hasSofa = true;
            return hasSofa;
        }
        
    }
}
